package com.situ.mall.quartz;

import com.situ.mall.config.RedisConstant;
import com.situ.mall.controller.QiniuUtils;
import com.situ.mall.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.Set;

@Configuration
public class DeleteImageJob {
    @Autowired
    private RedisTemplate redisTemplate;


    // <bean name="user" class="">
    @Bean
    public User createUser() {
        return new User();
    }
    @Scheduled(cron = "0 0/1 * * * ?")
    public void deleteImage() {
        System.out.println("DeleteImageJob.deleteImage");
        //Redis中保存的集合做差值计算删除没用的
        Set<String> set = redisTemplate.opsForSet().difference(RedisConstant.UPLOAD_IMAGE, RedisConstant.UPLOAD_IMAGE_TO_OB);
        if (set !=null){
            for (String imageName : set){
                //删除7牛没用的图片
                QiniuUtils.deleteFileFromQiniu(imageName);
                //redis里面存的没用的删除
                redisTemplate.opsForSet().remove(RedisConstant.UPLOAD_IMAGE,imageName);
                System.out.println("删除图片："+imageName);
            }
            redisTemplate.delete(RedisConstant.UPLOAD_IMAGE);
            redisTemplate.delete(RedisConstant.UPLOAD_IMAGE_TO_OB);
        }

    }
}
